<?php

/**
 *      [品牌空间] (C)2001-2010 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: consume.php 5723 2011-07-11 02:07:33Z menglingmin $
 */

define("CURSCRIPT", "consume");
require_once('./common.php');

$tpp = $_G['setting']['consumesearchperpage'];

$wheresql = '';
$where = array();
$orderby = 'i.displayorder ASC, ';

$keyword = trim($_G['gp_keyword']);

$catid = intval(trim($_REQUEST['catid']));
$tagids = '';

// Grade 必须大于 2
$where[] = 'i.grade_s > 2 AND i.grade > 2';

// 在有效期内的
if($_REQUEST['range'] == 'available') {
	$where[] = "i.validity_end > " . ($_G['timestamp'] - $_G['timestamp']%86400);
	$select['range'] = ' selected="selected"';
}
if(!empty($_REQUEST['orderBy']) && $_REQUEST['orderBy'] != 'dateline') {
	$select['byview'] = ' selected="selected"';
	$orderby .= 'i.viewnum';
} else {
	$select['bytime'] = ' selected="selected"';
	$orderby .= 'i.itemid';
}


// 读分类
$categorylist = getcategory('consume');

include_once('./batch.attribute.php');
$searchcats = getsearchcats($categorylist, $catid);

// 分类ID搜索条件
if(!empty($catid)) {
	$tagid[] = $catid;
	if(is_array($searchcats)) {
		foreach($searchcats as $key=>$value) {
			$tagid[] = $value['catid'];
		}
	}
	$tagids = implode(",", $tagid);
	$where[] = 'i.catid IN (' . $tagids . ')';
} else {
	$where[] = 'i.catid > 0';
}

// 属性筛选器
$attrvalues = empty($_GET['params'])?array():getattrvalues($_GET['params']);
if($catid && $categorylist[$catid]['havechild'] == 0) {
	$attarray = formatattarr($catid, $attrvalues, $keyword, 'consume.php');
}

// 属性搜索
$attr_in = getattr_in($catid, $attrvalues);

// 条件拼合
$attr_in!==NULL && $where[] = $attr_in;
$where[] = 'i.grade_s > 2 AND i.grade > 2';
$keyword && $where[] = 'i.subject LIKE \'%'.$keyword.'%\'';
if($catid>0 && $tagids) {
	$where[] = 'i.catid IN ('.$tagids.')';
} else {
	$where[] = 'i.catid > 0';
}
$wheresql = implode(' AND ', $where);

// 查询分类结果
if(empty($catid) || $categorylist[$catid]['havechild']) {
	$_BCACHE->cachesql('catnums', 'SELECT COUNT(i.itemid) as count, i.catid FROM '.DB::table('consumeitems').' i WHERE '.$wheresql.' GROUP BY i.catid', 0, 0, 100, 0, 'sitelist', 'consume');
	foreach($_SBLOCK['catnums'] as $value) {
		$catnums[$value['catid']] = intval($value['count']);
	}
	if(is_array($searchcats)) {
		foreach($searchcats as $key=>$value) {
			$catnums = getcatcount($value['catid'], $catnums);
		}
	}
}

// 分类面包屑导航
require_once(B_ROOT.'./source/function/nav.func.php');
$catnavarray = getcatnavarray($catid, 'consume');
$samelevelcatarray = getcategorychild($catid, 'consume', $catid);

$_BCACHE->cachesql('consumesearch', 'SELECT i.itemid, i.shopid FROM '.DB::table('consumeitems')." i WHERE $wheresql ORDER BY $orderby DESC", 0, 1, $tpp, 0, 'sitelist', 'consume');
$multipage = $_SBLOCK['consumesearch_multipage'];
$resultcount = $_SBLOCK['consumesearch_listcount'];
foreach($_SBLOCK['consumesearch'] as $result) {
	$result = $_BCACHE->getiteminfo('consume', $result['itemid'], $result['shopid']);
	$result['summary'] = cutstr($result['subject'], 18, true);
	$result['shopinfo'] = $_BCACHE->getshopinfo($result['shopid']);
	$list[] = $result;
}

// 用于模板中显示
$keyword_show = htmlspecialchars($keyword);

$active['consume'] = ' class="active"';
$location['name'] = (!$keyword_show ? '' : $keyword_show . ' - ') . $_G['setting']['site_nav']['consume']['name'];

$seo_title = ($catid == 0 ? "" : $categorylist[$catid]['name'] . " - ") . $location['name'] . " - " . $seo_title;
include template('templates/site/default/consumesearch.html.php', 1);

ob_out();

?>